package nbcp.base.config


import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport

/**
 * Created by udi on 2017.3.11.
 */

@Configuration
open class MyWebMvcConfig : WebMvcConfigurationSupport() {
    companion object {
        private val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }


    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

//    @Bean
//    open fun abc(): def {
//        println("abc")
//        return def();
//    }
//
//    class def : RequestMappingHandlerAdapter() {
//        override fun afterPropertiesSet() {
//            super.afterPropertiesSet()
//
//            //反射得到 argumentResolvers HandlerMethodArgumentResolverComposite
//
//            var adpterField_Resolvers = this::class.java.superclass.getDeclaredField("argumentResolvers");
//            adpterField_Resolvers.isAccessible = true;
//
//            var adpterField_Resolvers_Value0 = adpterField_Resolvers.get(this);
//            if (adpterField_Resolvers_Value0 == null) return;
//
//            var adpterField_Resolvers_Value = adpterField_Resolvers_Value0 as HandlerMethodArgumentResolverComposite;
//
//            var compositeField_Resolvers = adpterField_Resolvers_Value::class.java.getDeclaredField("argumentResolvers")
//            compositeField_Resolvers.isAccessible = true;
//
//            var compositeField_Resolvers_Value0 = compositeField_Resolvers.get(adpterField_Resolvers_Value);
//
//            if (compositeField_Resolvers_Value0 == null) return;
//
//            var compositeField_Resolvers_Value = compositeField_Resolvers_Value0 as LinkedList<HandlerMethodArgumentResolver>;
//
//            compositeField_Resolvers_Value.forEach {
//                println(it::class.java.name)
//            }
//        }
//    }


    @Value("\${server.servlet.session.cookie.name:}")
    var SessionKey: String = "";

//    @Bean
//    fun defaultCookieSerializer(): DefaultCookieSerializer {
//        var ret = DefaultCookieSerializer();
//        ret.setCookieName(SessionKey);
//        return ret;
//    }
//
//    @Bean
//    fun configureRedisAction(): ConfigureRedisAction {
//        return ConfigureRedisAction.NO_OP;
//    }

//    @Autowired
//    lateinit var filter: MyAllFilter
//
//    @Bean
//    fun allFilter(): FilterRegistrationBean<MyAllFilter> {
//        val registrationBean = FilterRegistrationBean<MyAllFilter>()
//        registrationBean.order = 10
//
//        registrationBean.filter = filter
//        val urlPatterns = ArrayList<String>()
//        urlPatterns.add("/*")
//        urlPatterns.add("/**")
//        registrationBean.urlPatterns = urlPatterns
//        return registrationBean
//    }

    @Autowired
    lateinit var myInterceptor: MyInterceptor

    override fun addInterceptors(registry: InterceptorRegistry) {


        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/*")
                .addPathPatterns("/**")
    }


//    @Bean
//    fun RegisteAuthImageServlet(): ServletRegistrationBean<AuthImageServlet> {
//        var registration = ServletRegistrationBean(AuthImageServlet())
//        registration.isEnabled = true
//        registration.addUrlMappings("/open/getValidateCodeImage")
//        return registration
//    }

//    class ObjectIdSerializer : ObjectSerializer {
//        override fun write(serializer: JSONSerializer?, value: Any?, filedName: Any?, fieldType: Type?, features: Int) {
//            val out = serializer!!.getWriter()
//            if (value == null) {
//                out.writeNull()
//                return
//            }
//
//
//            out.writeString(value.toString())
//        }
//    }


//    class DateConverter : AbstractHttpMessageConverter<LocalDateTime>() {
//        override fun writeInternal(p0: LocalDateTime, p1: HttpOutputMessage) {
//            p1?.body?.write(p0.AsString().toByteArray(Charset.forName("utf-8")))
//        }
//
//        override fun readInternal(p0: Class<out LocalDateTime>, p1: HttpInputMessage): LocalDateTime {
//            println("-----------")
//            return LocalDateTime.MIN
//        }
//
//        override fun supports(p0: Class<*>): Boolean {
//            if (p0 == LocalDateTime::class.java) return true;
//            return false;
//        }
//
//
//    }


//    class ddd:WebArgumentResolver{
//        override fun resolveArgument(methodParameter: MethodParameter?, webRequest: NativeWebRequest?): Any {
//            println("----------------WebArgumentResolver-----------------")
//            println(methodParameter)
//            return UNRESOLVED;
//        }
//    }
//
//    @Bean
//    fun abc():WebArgumentResolver{
//        var abcd =ddd();
//        return abcd;
//    }

//    class MvcMapResolver : HandlerMethodArgumentResolver {
//
//        //这是从 Cache 中来的， 所以不能从 HttpContext中获取进行判断
//        override fun supportsParameter(parameter: MethodParameter?): Boolean {
//            if (parameter == null) return false;
//            if (parameter.parameterName == null) return false;
//            if (parameter.parameterType is JsonMap) return true;
//            return false;
//        }
//
//        override fun resolveArgument(parameter: MethodParameter?, mavContainer: ModelAndViewContainer?, webRequest: NativeWebRequest?, binderFactory: WebDataBinderFactory?): Any {
//            if (parameter == null || parameter.parameterName == null) return JsonMap();
//            var request = webRequest!!.nativeRequest as MyHttpRequestWrapper;
//            return request.json;//String(request!!.inputStream.readBytes(), Charset.defaultCharset()).FromJson<JsonMap>();
//        }
//    }
//
//    override fun addArgumentResolvers(argumentResolvers: MutableList<HandlerMethodArgumentResolver>?) {
//        argumentResolvers?.add(MvcMapResolver())
//        super.addArgumentResolvers(argumentResolvers)
//    }
}